home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / filextrt.zip / FILEXTRT.C < prev    next >
Text File  |  1991-08-24  |  4KB  |  174 lines

  1. /* extract bytes from a file into another file */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <fcntl.h>
  7.  
  8. #ifdef MSDOS
  9. #include <io.h>
  10. #include <stdlib.h>
  11. #define BUF_SIZE    16384
  12. #else
  13. #define O_BINARY    0
  14. #define SEEK_SET    0
  15. #define SEEK_END    2
  16. #define BUF_SIZE    262144
  17. #endif
  18.  
  19. #define DEFAULT_BLOCK    262144
  20. int in_file, out_file;
  21. long start;
  22. long endf;
  23. long in_size;
  24. long bytes_to_copy;
  25.  
  26. char *buf;
  27. char in_file_name[128], out_file_name[128];
  28.  
  29. long size_of_block;
  30. int  file_count=0;
  31.  
  32. /* because ASCII C atol does not distinguish between zero and errors */
  33. long myatol (s) char *s; {
  34.     long temp;
  35.     temp = 0;
  36.     while (*s != 0) {
  37.         if (*s<'0' || *s>'9')    return (-1);
  38.         temp = 10*temp + (*s-'0');
  39.         s++;
  40.     }
  41.     return (temp);
  42. } /* end myatol */
  43.  
  44.  
  45. int do_the_copy () {
  46.     long bytes_read;
  47.     int  result;
  48.     int  read_size;
  49.  
  50.     for (bytes_read=0; bytes_read<bytes_to_copy; bytes_read+=result) {
  51.  
  52.         if (bytes_to_copy - bytes_read < BUF_SIZE)
  53.             read_size = bytes_to_copy - bytes_read;
  54.         else
  55.             read_size = BUF_SIZE;
  56.  
  57.         if ( (result=read (in_file, buf, read_size)) != read_size) {
  58.             fprintf (stderr, "read error\n");
  59.             return (1);
  60.         }
  61.  
  62.         if (write (out_file, buf, result) < 0) {
  63.             printf ("write error\n");
  64.             return (1);
  65.         }
  66.     }
  67.     return (0);
  68. }
  69.  
  70.  
  71. void usage () {
  72.     fprintf (stderr, "\nfilextrt 1.0 - extract bytes from a file. Richard Marks\n\n");
  73.     fprintf (stderr, "filextrt [-s s]  [-e e] [-n n] infile outfile\n");
  74.     fprintf (stderr, "\tstarting at byte offest s from the front of the file,\n");
  75.     fprintf (stderr, "\tor byte offset e from the end of the file\n");
  76.     fprintf (stderr, "\textracts n bytes from infile result to outfile\n");
  77.     fprintf (stderr, "\ts and e are not specified, defaults to the start of the file.\n");
  78.     fprintf (stderr, "\tif n is not specified, extract the remainder of the file\n");
  79.     fprintf (stderr, "\tNote, start and end are offsets, start 1 is second byte\n");
  80.     exit (1);
  81. }  /* end usage */
  82.  
  83.  
  84. void main (argc, argv) int argc; char **argv; {
  85. int i=1;
  86. in_file_name[0] = out_file_name[0] = 0;
  87.  
  88. start = -1;
  89. endf  = -1;
  90. in_size = 0;
  91. bytes_to_copy = -1;
  92.  
  93. while (i<argc) {
  94.     if (argv[i][0] != '-')    break;
  95.     if (argv[i][2] != 0)    usage ();
  96.     if (tolower(argv[i][1]) == 's') {
  97.         if (start != -1 || endf != -1)    usage ();
  98.         if (++i >= argc)        usage ();
  99.         if ((start=myatol(argv[i])) <0)    usage ();
  100.  
  101.     } else if (tolower(argv[i][1]) == 'e') {
  102.         if (start != -1 || endf != -1)    usage ();
  103.         if (++i >= argc)        usage ();
  104.         if ((endf=myatol(argv[i])) < 0)    usage ();
  105.  
  106.     } else if (tolower(argv[i][1]) == 'n') {
  107.         if (bytes_to_copy != -1)    usage ();
  108.         if (++i >= argc)        usage ();
  109.         if ((bytes_to_copy=myatol(argv[i])) <= 0)    usage ();
  110.     } else {
  111.         usage ();
  112.     }
  113.     i++;
  114. }
  115.  
  116. /* get input file name - if given */
  117. if (i<argc) {
  118.     strcpy (in_file_name, argv[i]);
  119.     if ( (in_file=open (in_file_name, O_RDONLY | O_BINARY)) < 0) {
  120.         fprintf (stderr, "open error on %s\n", in_file_name);
  121.         usage ();
  122.     }
  123. }
  124.  
  125. i++;
  126. if (i<argc) {
  127.     strcpy (out_file_name, argv[i]);
  128.     if (strcmp(out_file_name, "-") == 0)  out_file_name[0]=0;
  129. }
  130.  
  131. i++;
  132. if (i != argc)    usage ();
  133. if (start==-1 && bytes_to_copy==-1 && endf==-1) usage ();
  134.  
  135. in_size = lseek (in_file, 0, SEEK_END);
  136. if (endf >= 0) {
  137.     if (endf >= in_size)    goto E1;
  138.     start=in_size-endf-1;
  139. }
  140. if (start<0)        start=0;
  141. if (start>=in_size)    goto E1;
  142. if (bytes_to_copy<=0)    bytes_to_copy = in_size-start;
  143.  
  144. if (start+bytes_to_copy > in_size) {
  145. E1:    fprintf (stderr, "param greater than file size of %d\n", in_size);
  146.     usage ();
  147. }
  148.  
  149. if ((buf=(char*)malloc(BUF_SIZE)) == NULL) {
  150.     fprintf (stderr, "memory allocation error\n");
  151.     usage ();
  152. }
  153.  
  154. if ( (out_file=open (out_file_name, O_CREAT|O_WRONLY|O_BINARY, 0666)) < 0) {
  155.         fprintf (stderr, "open error on %s\n", out_file_name);
  156.         usage ();
  157. }
  158.  
  159. /* now, position for reads */
  160. if (lseek (in_file, start, SEEK_SET) != start) {
  161.         fprintf (stderr, "lseek error\n");
  162.         usage ();
  163. }
  164.  
  165. /* print summary of what is happening */
  166. fprintf (stderr, "filextrt 1.0, by Richard Marks\n");
  167. fprintf (stderr, "Extracting %ld bytes starting at byte %ld, from file %s\n",
  168.             bytes_to_copy, start, in_file_name);
  169.  
  170. do_the_copy ();
  171.  
  172. close (in_file);  close(out_file);
  173. }
  174.